What is apollo-server-express?
The apollo-server-express package is a library that allows you to integrate Apollo Server with an Express application. It provides tools to build a GraphQL server with Express, enabling you to define your GraphQL schema, resolvers, and context, and to handle HTTP requests and responses.
What are apollo-server-express's main functionalities?
Setting up a basic Apollo Server with Express
This code sets up a basic Apollo Server with Express. It defines a simple GraphQL schema with a single query and a resolver for that query. The Apollo Server is then applied as middleware to the Express app, and the server is started on port 4000.
const express = require('express');
const { ApolloServer, gql } = require('apollo-server-express');
const typeDefs = gql`
type Query {
hello: String
}
`;
const resolvers = {
Query: {
hello: () => 'Hello world!',
},
};
const server = new ApolloServer({ typeDefs, resolvers });
const app = express();
server.applyMiddleware({ app });
app.listen({ port: 4000 }, () =>
console.log(`Server ready at http://localhost:4000${server.graphqlPath}`)
);
Adding context to Apollo Server
This code demonstrates how to add context to the Apollo Server. The context function extracts the user from the request headers and makes it available to the resolvers. The resolver for the 'hello' query uses the context to personalize the response.
const express = require('express');
const { ApolloServer, gql } = require('apollo-server-express');
const typeDefs = gql`
type Query {
hello: String
}
`;
const resolvers = {
Query: {
hello: (parent, args, context) => `Hello ${context.user}!`,
},
};
const server = new ApolloServer({
typeDefs,
resolvers,
context: ({ req }) => ({ user: req.headers.user || 'world' })
});
const app = express();
server.applyMiddleware({ app });
app.listen({ port: 4000 }, () =>
console.log(`Server ready at http://localhost:4000${server.graphqlPath}`)
);
Using Apollo Server with Express middleware
This code shows how to use Apollo Server with other Express middleware. In this example, the body-parser middleware is used to parse JSON request bodies before the Apollo Server middleware is applied.
const express = require('express');
const { ApolloServer, gql } = require('apollo-server-express');
const bodyParser = require('body-parser');
const typeDefs = gql`
type Query {
hello: String
}
`;
const resolvers = {
Query: {
hello: () => 'Hello world!',
},
};
const server = new ApolloServer({ typeDefs, resolvers });
const app = express();
app.use(bodyParser.json());
server.applyMiddleware({ app });
app.listen({ port: 4000 }, () =>
console.log(`Server ready at http://localhost:4000${server.graphqlPath}`)
);
Other packages similar to apollo-server-express
express-graphql
The express-graphql package is a middleware for Express that allows you to create a GraphQL HTTP server. It is simpler and more lightweight compared to apollo-server-express, but it lacks some of the advanced features and integrations provided by Apollo Server, such as schema stitching, Apollo Federation, and built-in support for subscriptions.
graphql-yoga
graphql-yoga is a fully-featured GraphQL server that works with any GraphQL schema. It is built on top of Express and apollo-server, and it provides a simple and flexible API for building GraphQL servers. Compared to apollo-server-express, graphql-yoga offers a more opinionated setup with built-in support for features like subscriptions, file uploads, and GraphQL Playground.
mercurius
mercurius is a GraphQL adapter for Fastify, a fast and low-overhead web framework for Node.js. It provides a similar set of features to apollo-server-express, including schema stitching, subscriptions, and context management. However, it is designed to work specifically with Fastify, which offers better performance and scalability compared to Express.
title: Express / Connect
description: Setting up Apollo Server with Express.js or Connect
This is the Express and Connect integration of GraphQL Server. Apollo Server is a community-maintained open-source GraphQL server that works with all Node.js HTTP server frameworks: Express, Connect, Hapi, Koa and Restify. Read the docs. Read the CHANGELOG.
npm install apollo-server-express
Express
import express from 'express';
import bodyParser from 'body-parser';
import { graphqlExpress } from 'apollo-server-express';
const myGraphQLSchema =
const PORT = 3000;
const app = express();
app.use('/graphql', bodyParser.json(), graphqlExpress({ schema: myGraphQLSchema }));
app.listen(PORT);
Connect
import connect from 'connect';
import bodyParser from 'body-parser';
import { graphqlConnect } from 'apollo-server-express';
import http from 'http';
const PORT = 3000;
const app = connect();
app.use('/graphql', bodyParser.json());
app.use('/graphql', graphqlConnect({ schema: myGraphQLSchema }));
http.createServer(app).listen(PORT);
Principles
GraphQL Server is built with the following principles in mind:
- By the community, for the community: GraphQL Server's development is driven by the needs of developers
- Simplicity: by keeping things simple, GraphQL Server is easier to use, easier to contribute to, and more secure
- Performance: GraphQL Server is well-tested and production-ready - no modifications needed
Anyone is welcome to contribute to GraphQL Server, just read CONTRIBUTING.md, take a look at the roadmap and make your first PR!